<template>
  <div class="revenue-dashboard">
    <div class="dashboard-header">
      <h3 class="dashboard-title">营收仪表盘</h3>
      <div class="dashboard-status">
        <span class="target-info">目标达成 {{ achievementRate }}%</span>
      </div>
    </div>
    
    <div class="dashboard-content">
      <!-- 主要进度环 -->
      <div class="main-progress">
        <div class="progress-circle">
          <svg viewBox="0 0 120 120" class="progress-svg">
            <defs>
              <linearGradient id="progressGradient" x1="0%" y1="0%" x2="100%" y2="100%">
                <stop offset="0%" style="stop-color:#00f0ff" />
                <stop offset="100%" style="stop-color:#00ff9d" />
              </linearGradient>
            </defs>
            <!-- 背景圆环 -->
            <circle 
              cx="60" cy="60" r="50" 
              fill="none" 
              stroke="rgba(0, 240, 255, 0.1)" 
              stroke-width="8"
            />
            <!-- 进度圆环 -->
            <circle 
              cx="60" cy="60" r="50" 
              fill="none" 
              stroke="url(#progressGradient)" 
              stroke-width="8"
              stroke-linecap="round"
              :stroke-dasharray="circumference"
              :stroke-dashoffset="circumference - (achievementRate / 100) * circumference"
              class="progress-ring"
            />
          </svg>
          <div class="progress-text">
            <div class="progress-value">{{ achievementRate }}%</div>
            <div class="progress-label">目标完成</div>
          </div>
        </div>
      </div>
      
      <!-- KPI指标 -->
      <div class="kpi-indicators">
        <div 
          v-for="(kpi, index) in kpiData" 
          :key="index"
          class="kpi-item"
          :style="{ animationDelay: index * 100 + 'ms' }"
        >
          <div class="kpi-icon" :style="{ background: kpi.color }">
            <i :class="kpi.icon"></i>
          </div>
          <div class="kpi-content">
            <div class="kpi-value">
              <AnimatedCounter 
                :value="kpi.value" 
                :prefix="kpi.prefix" 
                :suffix="kpi.suffix"
                :decimals="kpi.decimals || 0"
              />
            </div>
            <div class="kpi-label">{{ kpi.label }}</div>
            <div class="kpi-trend" :class="kpi.trend > 0 ? 'positive' : 'negative'">
              {{ kpi.trend > 0 ? '↗' : '↘' }} {{ Math.abs(kpi.trend) }}%
            </div>
          </div>
          <div class="kpi-progress">
            <div 
              class="progress-bar" 
              :style="{ 
                width: kpi.progress + '%',
                background: kpi.color 
              }"
            ></div>
          </div>
        </div>
      </div>
      
      <!-- 实时指标 -->
      <div class="realtime-metrics">
        <div class="metrics-header">
          <span class="metrics-title">实时指标</span>
          <span class="pulse-indicator">
            <span class="pulse-dot"></span>
            Live
          </span>
        </div>
        
        <div class="metrics-grid">
          <div 
            v-for="(metric, index) in realtimeMetrics" 
            :key="index"
            class="metric-card"
          >
            <div class="metric-name">{{ metric.name }}</div>
            <div class="metric-value" :style="{ color: metric.color }">
              {{ metric.value }}{{ metric.unit }}
            </div>
            <div class="metric-spark">
              <svg viewBox="0 0 60 20" class="spark-svg">
                <path 
                  :d="generateSparkPath(metric.history)" 
                  fill="none" 
                  :stroke="metric.color" 
                  stroke-width="2"
                />
              </svg>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted, onUnmounted, computed } from 'vue'
import AnimatedCounter from './AnimatedCounter.vue'

// 目标完成率
const achievementRate = ref(76.8)

// 圆环周长
const circumference = computed(() => 2 * Math.PI * 50)

// KPI数据
const kpiData = ref([
  {
    label: '日营收',
    value: 128500,
    prefix: '¥',
    suffix: '',
    decimals: 0,
    progress: 82,
    trend: 15.2,
    color: '#00f0ff',
    icon: 'fas fa-coins'
  },
  {
    label: '月增长',
    value: 23.6,
    prefix: '',
    suffix: '%',
    decimals: 1,
    progress: 65,
    trend: 8.7,
    color: '#00ff9d',
    icon: 'fas fa-chart-line'
  },
  {
    label: '客单价',
    value: 165.8,
    prefix: '¥',
    suffix: '',
    decimals: 1,
    progress: 78,
    trend: -2.3,
    color: '#ffa502',
    icon: 'fas fa-user-circle'
  },
  {
    label: '转化率',
    value: 4.2,
    prefix: '',
    suffix: '%',
    decimals: 1,
    progress: 89,
    trend: 12.1,
    color: '#ff6b6b',
    icon: 'fas fa-exchange-alt'
  }
])

// 实时指标
const realtimeMetrics = ref([
  {
    name: 'QPS',
    value: 1247,
    unit: '/s',
    color: '#00f0ff',
    history: []
  },
  {
    name: 'RT',
    value: 156,
    unit: 'ms',
    color: '#00ff9d',
    history: []
  },
  {
    name: '在线',
    value: 8924,
    unit: '人',
    color: '#ffa502',
    history: []
  },
  {
    name: '错误率',
    value: 0.02,
    unit: '%',
    color: '#ff4757',
    history: []
  }
])

let updateTimer = null

// 生成火花图路径
const generateSparkPath = (history) => {
  if (history.length === 0) return ''
  
  const width = 60
  const height = 20
  const step = width / (history.length - 1)
  
  let path = `M 0 ${height - (history[0] / 100) * height}`
  
  for (let i = 1; i < history.length; i++) {
    const x = i * step
    const y = height - (history[i] / 100) * height
    path += ` L ${x} ${y}`
  }
  
  return path
}

// 更新数据
const updateData = () => {
  // 更新目标完成率
  achievementRate.value += (Math.random() - 0.5) * 2
  achievementRate.value = Math.max(0, Math.min(100, achievementRate.value))
  
  // 更新KPI数据
  kpiData.value.forEach(kpi => {
    const change = (Math.random() - 0.5) * 0.1
    kpi.value *= (1 + change)
    kpi.progress += (Math.random() - 0.5) * 5
    kpi.progress = Math.max(0, Math.min(100, kpi.progress))
  })
  
  // 更新实时指标
  realtimeMetrics.value.forEach(metric => {
    const change = (Math.random() - 0.5) * 0.2
    metric.value *= (1 + change)
    metric.value = Math.round(metric.value * 100) / 100
    
    // 更新历史数据
    metric.history.push(Math.random() * 100)
    if (metric.history.length > 15) {
      metric.history.shift()
    }
  })
}

// 初始化历史数据
const initializeHistory = () => {
  realtimeMetrics.value.forEach(metric => {
    metric.history = Array.from({ length: 15 }, () => Math.random() * 100)
  })
}

onMounted(() => {
  initializeHistory()
  updateTimer = setInterval(updateData, 1500)
})

onUnmounted(() => {
  if (updateTimer) {
    clearInterval(updateTimer)
  }
})
</script>

<style scoped>
.revenue-dashboard {
  height: 100%;
  display: flex;
  flex-direction: column;
  background: rgba(15, 52, 96, 0.3);
  border-radius: 12px;
  border: 1px solid rgba(0, 240, 255, 0.2);
  backdrop-filter: blur(10px);
  padding: 20px;
  overflow: hidden;
}

.dashboard-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 16px;
  padding-bottom: 12px;
  border-bottom: 1px solid rgba(0, 240, 255, 0.1);
}

.dashboard-title {
  color: #00f0ff;
  font-size: 18px;
  font-weight: 600;
  margin: 0;
}

.target-info {
  color: #00ff9d;
  font-size: 11px;
  font-weight: 500;
}

.dashboard-content {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.main-progress {
  display: flex;
  justify-content: center;
  margin-bottom: 8px;
}

.progress-circle {
  position: relative;
  width: 120px;
  height: 120px;
}

.progress-svg {
  width: 100%;
  height: 100%;
  transform: rotate(-90deg);
}

.progress-ring {
  transition: stroke-dashoffset 1s ease-in-out;
}

.progress-text {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  text-align: center;
}

.progress-value {
  color: #00f0ff;
  font-size: 20px;
  font-weight: 700;
  margin-bottom: 2px;
}

.progress-label {
  color: rgba(255, 255, 255, 0.7);
  font-size: 10px;
}

.kpi-indicators {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
  margin-bottom: 12px;
}

.kpi-item {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px;
  background: rgba(0, 240, 255, 0.05);
  border-radius: 8px;
  border: 1px solid rgba(0, 240, 255, 0.1);
  animation: slideInUp 0.6s ease-out both;
  position: relative;
  overflow: hidden;
}

@keyframes slideInUp {
  from {
    opacity: 0;
    transform: translateY(20px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

.kpi-icon {
  width: 24px;
  height: 24px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  color: white;
  font-size: 10px;
  flex-shrink: 0;
}

.kpi-content {
  flex: 1;
  min-width: 0;
}

.kpi-value {
  color: #fff;
  font-size: 12px;
  font-weight: 600;
  margin-bottom: 2px;
}

.kpi-label {
  color: rgba(255, 255, 255, 0.7);
  font-size: 9px;
  margin-bottom: 2px;
}

.kpi-trend {
  font-size: 8px;
  font-weight: 500;
}

.kpi-trend.positive {
  color: #00ff9d;
}

.kpi-trend.negative {
  color: #ff4757;
}

.kpi-progress {
  position: absolute;
  bottom: 0;
  left: 0;
  right: 0;
  height: 2px;
}

.progress-bar {
  height: 100%;
  transition: width 1s ease;
  border-radius: 1px;
}

.realtime-metrics {
  flex: 1;
  min-height: 0;
}

.metrics-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 8px;
}

.metrics-title {
  color: #00f0ff;
  font-size: 12px;
  font-weight: 600;
}

.pulse-indicator {
  display: flex;
  align-items: center;
  gap: 4px;
  color: #00ff9d;
  font-size: 9px;
  font-weight: 500;
}

.pulse-dot {
  width: 4px;
  height: 4px;
  background: #00ff9d;
  border-radius: 50%;
  animation: pulse-glow 2s infinite;
}

@keyframes pulse-glow {
  0%, 100% { box-shadow: 0 0 0 0 rgba(0, 255, 157, 0.7); }
  70% { box-shadow: 0 0 0 4px rgba(0, 255, 157, 0); }
}

.metrics-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
}

.metric-card {
  padding: 8px;
  background: rgba(0, 240, 255, 0.05);
  border-radius: 6px;
  border: 1px solid rgba(0, 240, 255, 0.1);
  text-align: center;
}

.metric-name {
  color: rgba(255, 255, 255, 0.7);
  font-size: 9px;
  margin-bottom: 4px;
}

.metric-value {
  font-size: 11px;
  font-weight: 600;
  margin-bottom: 4px;
}

.metric-spark {
  height: 20px;
}

.spark-svg {
  width: 100%;
  height: 100%;
}
</style> 